package com.wyw.kafka.learning.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wyw.kafka.learning.producer.KafkaProducer;
import com.wyw.kafka.learning.vo.WywMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title   kafka生成数据controller
 * @Description
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
@Slf4j
@RestController
@RequestMapping("/kafka")
@RequiredArgsConstructor
public class KafkaController {
    private final ObjectMapper mapper;
    private final KafkaProducer kafkaProducer;

    /**
     * 发送 kafka 消息
     * */
    @GetMapping("/send-message")
    public void sendMessage(@RequestParam(required = false) String key,
                            @RequestParam String topic) throws Exception {

        WywMessage message = new WywMessage(
                1,
                "wyw-kafka-study"
        );
        kafkaProducer.sendMessage(key, mapper.writeValueAsString(message), topic);
    }
}

